package com.concurrent.aqs;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * @author: zhouyang
 * @descrpition: 用Future获取一个线程返回的值
 * @time: 2019-05-10 12:02
 */
@Slf4j
public class FutureTest {

    static class MyCallable implements Callable<String> {
        @Override
        public String call() throws Exception {
            log.info("my callable...");
            Thread.sleep(3000);
            return "okay";
        }
    }

    public static void main(String[] args) throws Exception{
        ExecutorService executorService = Executors.newCachedThreadPool();
        Future<String> res = executorService.submit(new MyCallable());
        log.info("future get {}",res.get());//如果任务没有结束 那么线程会阻塞在这里
        executorService.shutdown();
    }
}
